import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

const mockProxy = {
  '/v/': {
    bypass(req, res) {
      if (req.method !== 'GET' || req.url !== '/v/order') {
        res.end()
        return false
      }
      const data = [{
        id: 0,
        Command_name: '上天',
        desc: 'fly',
      }, {
        id: 1,
        Command_name: '入海',
        desc: 'dive',
      }, {
        id: 2,
        Command_name: '狂奔',
        desc: 'rush',
      }, {
        id: 3,
        Command_name: '怒号',
      }]
      res.setHeader('Content-Type', 'application/json')
      res.end(JSON.stringify(data), 'utf8')
      return false
    },
  },
}

const env: ImportMetaEnv = loadEnv('mock', process.cwd())
const proxy = {
  '/v/': env.VITE_HOST,
}

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
  plugins: [vue()],
  server: {
    proxy: mode === 'mock' ? mockProxy : proxy,
  },
}))
